@push('title')
    <title>数据抓取任务列表</title>
@endpush
@push('style2')
<style>


    .layui-table-cell{
        height: auto;
        overflow:visible;
        text-overflow:inherit;
        white-space:normal;
        word-break: break-all;
    }
</style>
@endpush
@extends('layout.layer')
@section('content')

    <form class="layui-form layui-form-pane1" action="" lay-filter="first" style="margin-bottom: 15px">
        <div class="layui-input-inline">
            <div class="layui-input-inline">
                <div class="layui-input-inline">
                    <input type="text" name="id" maxlength="50" placeholder="航线ID" autocomplete="off" class="layui-input" >
                </div>
            </div>

            <div class="layui-input-inline">
                <div class="layui-input-inline">
                    <input type="text" name="airline" maxlength="50" placeholder="航司编号" autocomplete="off" class="layui-input" >
                </div>
            </div>

            <div class="layui-input-inline">
                <div class="layui-input-inline">
                    <input type="text" name="depart_code" maxlength="3" placeholder="三字码" autocomplete="off" class="layui-input" >
                </div>
            </div>

            <div class="layui-input-inline">
                <div class="layui-input-inline">
                    <input type="text" name="arrived_code" maxlength="3" placeholder="三字码" autocomplete="off" class="layui-input" >
                </div>
            </div>

            <div class="layui-input-inline">
                <div class="layui-input-inline">
                    <input type="text" name="flight_code" maxlength="6" placeholder="航班" autocomplete="off" class="layui-input" >
                </div>
            </div>

            <div class="layui-input-inline">
                <button class="layui-btn" id="search-btn" lay-submit lay-filter="*">搜索</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                <a class="layui-btn" id="download" >下载</a>
            </div>
        </div>
    </form>
    <table id="test2" lay-filter="test2"></table>
@endsection
@push('script')
    <script>

        layui.use(['table','form','laydate'], function() {
            var $ = layui.$
                ,table = layui.table
                ,form = layui.form
                ,laydate = layui.laydate;


            //日期
            laydate.render({
                elem: '#day'
                ,type: 'date'
            });


            var tableData = {};

            table.render({
                id: 'test2'
                ,elem: '#test2'
                ,url: '/javaapi/data/crmTasks/query'
                ,page: { //详细参数可参考 laypage 组件文档
                    curr: 1
                    ,groups: 20
                    ,first: 1
                    ,last: true
                    ,layout: ['limit', 'prev', 'page', 'next', 'count','refresh', 'skip'] //自定义分页布局
                }
                //,height: 300
                ,cellMinWidth: 80
                ,limit : 30
                //,skin: 'line'
                ,toolbar: false
                ,parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                    tableData =  res.data

                    return {
                        "code": res.errcode, //解析接口状态
                        "msg": res.errmsg, //解析提示文本
                        "count": res.meta.total, //解析数据长度
                        "data":res.data,
                    };


                }
                ,request: {
                    pageName: 'page' //页码的参数名称，默认：page
                    ,limitName: 'page_size' //每页数据量的参数名，默认：limit
                }
                ,cols: [[
                    {field: 'id', 'title':'ID'}
                    ,{field: 'flightCode', 'title':'航班',templet:function (row) {
                            return row.flight_code+row.flight_line.hs_code;
                        }}
                    ,{field: 'depart_city_code', 'title':'起飞',templet: function (row) {
                            return row.flight_line.depart_city_code
                        }}
                    ,{field: 'arrived_city_code', 'title':'到达',templet: function (row) {
                            return row.flight_line.arrived_city_code
                        }}
                    ,{field: 'start_at', 'title':'日期'}
                    ,{field: 'cabin', 'title':'舱位',templet: function (row) {
                            return row.cabins[0].cabin_code
                        }}
                    ,{field: 'price', 'title':'价格',templet: function (row) {
                            return row.cabins[0].price
                        }}
                    ,{field: 'rate', 'title':'代理费',templet: function (row) {
                            return row.cabins[0].rate == null ? 0 :  row.cabins[0].rate;
                        }}
                    ,{field: 'flight_min_price', 'title':'高舱高反金额',templet: function (row) {
                            return row.cabins[0].flight_min_price == null ? 0 :  row.cabins[0].flight_min_price;
                        }}
                ]]

            });

            /**
             * 设置倒计时
             */
            setInterval(function () {

                var nowTime = new Date();
                for(var i = 0; i < tableData.length; i++) {

                    var orderTime = new Date(tableData[i].created_at);

                    var expireMinute = parseInt(((15 * 60 * 1000 - (nowTime - orderTime)) / 60000));

                    var expired = expireMinute + ':' + parseInt(((15 * 60 * 1000 - (nowTime - orderTime)) % 60000) / 1000);
                    if(expireMinute < 2) {

                        $('#expired_'+tableData[i].id).html(expired).attr("style","color: orangered");
                    } else {

                        $('#expired_'+tableData[i].id).html(expired)
                    }

                }

            },1000)

            //监听提交
            form.on('submit(*)', function(data){

                table.reload('test2',{
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where:data.field
                });

                return false;
            });

            //监听工具条
            table.on('tool(test2)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    ,layEvent = obj.event; //获得 lay-event 对应的值


                if (layEvent === 'policy-del') {

                    $.ajax({
                        url: '/policy/ajaxDelete?id='+data.id
                        , method: 'delete'
                        , data: {_token:"{{csrf_token()}}"}
                        , dataType: 'json'
                        , success: function (data) {

                            layer.msg(data.errmsg, {
                                offset: '15px'
                                , icon: data.errcode == 0 ? 1 : 15
                                , time: 3000
                            }, function () {

                            });

                            if(data.errcode == 0) {

                                table.reload("test2")
                            }

                        },
                    });
                } else if (layEvent === 'policy-update') {

                    window.open("/policy/update?id="+data.id);

                }  else if (layEvent === 'policy-status') {


                    $.ajax({
                        url: '/policy/itemUpdate'
                        , method: 'put'
                        , data: {id:data.id,status:data.status == 1 ? 2 : 1, _token:"{{csrf_token()}}" }
                        , dataType: 'json'
                        , success: function (res) {

                            layer.msg(res.errmsg, {
                                offset: '15px'
                                , icon: res.errcode == 0 ? 1 : 15
                                , time: 2000
                            }, function () {

                            });

                            if(res.errcode == 0) {

                                table.reload("test2")
                            }

                        },
                    });
                }

            });

            var jsonToParams = function (json) {
                var params = Object.keys(json).map(function(key) {
                    return encodeURIComponent(key) + '=' + encodeURIComponent(json[key]);
                }).join('&');
                return params;
            }

            $('#download').on('click',function () {

                var formData = form.val('first');
                var paramStr = jsonToParams(formData);

                window.location.href = '/scan/download?'+paramStr;
            });

        });
    </script>

@endpush